<?php

namespace app\api\controller;

use app\admin\logic\system\SystemConfigLogic;
use app\api\logic\WalletLogic;
use app\basic\ApiController;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("钱包接口")
 */
class WalletController extends ApiController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new WalletLogic();
        parent::__construct();
    }

    /**
     * @Apidoc\Title("获取钱包概览")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="钱包信息", children={
     *     @Apidoc\Returned("balance", type="string", desc="账户余额"),
     *     @Apidoc\Returned("commission", type="string", desc="可提现金额"),
     *     @Apidoc\Returned("total_commission", type="string", desc="累计佣金")
     * })
     */
    public function overview()
    {
        $data = $this->logic->getWalletOverview($this->auth->id);
        return $this->success($data);
    }

    /**
     * @Apidoc\Title("消费记录")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     */
    public function moneyLog()
    {
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 8);
        $list = $this->logic->getMoneyLog($this->auth->id, $page, $limit);
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("提现记录")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     */
    public function withdrawLog()
    {
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 8);
        $list = $this->logic->getWithdrawLog($this->auth->id, $page, $limit);
        return $this->success($list);
    }

     /**
     * @Apidoc\Title("佣金记录")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     * @Apidoc\Returned("data", type="object", desc="佣金记录", children={
     *     @Apidoc\Returned("list", type="array", desc="记录列表", children={
     *         @Apidoc\Returned("id", type="int", desc="记录ID"),
     *         @Apidoc\Returned("money", type="string", desc="变动金额"),
     *         @Apidoc\Returned("title", type="string", desc="变动说明"),
     *         @Apidoc\Returned("memo", type="string", desc="备注"),
     *         @Apidoc\Returned("create_time", type="string", desc="创建时间")
     *     }),
     *     @Apidoc\Returned("total", type="int", desc="总记录数"),
     *     @Apidoc\Returned("page", type="int", desc="当前页码"),
     *     @Apidoc\Returned("limit", type="int", desc="每页数量")
     * })
     */
    public function commissionLog()
    {
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 8);

        $list = $this->logic->getCommissionLog($this->auth->id, $page, $limit);
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("申请提现")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("money", type="float", require=true, desc="提现金额")
     * @Apidoc\Param("type", type="int", require=true, desc="提现方式：1=支付宝 2=微信")
     * @Apidoc\Param("account", type="string", require=true, desc="收款账号")
     * @Apidoc\Param("real_name", type="string", require=true, desc="真实姓名")
     * @Apidoc\Returned("data", type="boolean", desc="提现结果")
     */
    public function withdraw()
    {
        $params = $this->request->only(['money', 'type', 'account', 'real_name']);
        if (!$this->logic->createWithdraw($this->auth->id, $params)) {
            return $this->fail($this->logic->getError());
        }
        return $this->success('提现申请成功');
    }

    /**
     * @Apidoc\Title("获取充值规格列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="充值规格列表", children={
     *     @Apidoc\Returned("id", type="int", desc="规格ID"),
     *     @Apidoc\Returned("amount", type="string", desc="充值金额"),
     * })
     */
    public function rechargeConfig()
    {
        $list = $this->logic->getRechargeConfig();
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("创建充值订单")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("config_id", type="int", require=true, desc="充值规格ID")
     * @Apidoc\Param("pay_type", type="int", require=true, desc="支付方式：1=支付宝 2=微信")
     * @Apidoc\Returned("data", type="object", desc="充值订单信息", children={
     *     @Apidoc\Returned("order_no", type="string", desc="充值订单号"),
     *     @Apidoc\Returned("money", type="string", desc="充值金额"),
     *     @Apidoc\Returned("pay_data", type="string", desc="支付参数")
     * })
     */
    public function createRecharge()
    {
        $params = $this->request->only(['config_id', 'pay_type']);
        if (!$result = $this->logic->createRecharge($this->auth->id, $params)) {
            return $this->fail($this->logic->getError());
        }
        return $this->success($result);
    }

    /**
     * @Apidoc\Title("提现规则")
     * @Apidoc\Method("POST")
     * })
     */
    public function getWithdrawRule()
    {
        $systemConfigLogic = new SystemConfigLogic();
        $systemInfo = $systemConfigLogic->getConfigByGroup('withdraw');
        return $this->success($systemInfo);
    }

} 